package com.huazai.service.impl;

import com.huazai.bean.po.Books;
import com.huazai.bean.po.Borrow;
import com.huazai.bean.vo.BorrowVO;
import com.huazai.dao.BooksMapper;
import com.huazai.dao.BorrowMapper;
import com.huazai.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Qualifier("BorrowServiceImpl")
public class BorrowServiceImpl implements BorrowService {

    private BorrowMapper borrowMapper;

    private BooksMapper booksMapper;

    @Autowired
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Autowired
    public void setBorrowMapper(BorrowMapper borrowMapper) {
        this.borrowMapper = borrowMapper;
    }

    @Override
    public List<BorrowVO> getBorrowCustom(String borrowerEmail) {
        return borrowMapper.getBorrowCustom(borrowerEmail, null);
    }

    @Override
    public Boolean deleteBorrowApply(String borrowID) {
        return borrowMapper.deleteBorrowApply(borrowID);
    }

    @Override
    public Boolean addBorrowApply(Borrow borrow) {
        return borrowMapper.addBorrowApply(borrow);
    }

    @Override
    public Boolean approveBorrowApply(Map<String, String> map) {
        return borrowMapper.approveBorrowApply(map);
    }

    @Override
    public Boolean restoreBookCount(String borrowID) {
        Long bookID = borrowMapper.getBookID(borrowID);

        Map<String, String> map = new HashMap<>();
        map.put("bookID", bookID.toString());

        Books books = booksMapper.selectBook(map).get(0);

        Integer bookCounts = books.getBookCounts();
        bookCounts ++;
        books.setBookCounts(bookCounts);

        return booksMapper.updateBook(books);
    }

    @Override
    public Integer deleteApplyCustom(Map<String, String> map) {
        return borrowMapper.deleteApplyCustom(map);
    }

    @Override
    public List<String> getUserAllBorrowID(String borrowerEmail) {
        return borrowMapper.getUserAllBorrowID(borrowerEmail);
    }
}
